users = set()

with open('from_DSM/DBscan_cluster_out5h6h40.txt', 'r') as f:
    with open('from_DSM/DBscan_cluster_out_new.txt', 'w') as out:
        line = f.readline()

        while True:
            if not line:
                break

            temp = line.strip().split(',')
            userId = int(temp[0])

            if userId not in users:
                new_cluster_count = 1
                users.add(userId)
            else:
                new_cluster_count += 1

            last_baseId = int(temp[2])
            start_time = int(temp[3])
            end_time = int(temp[4])

            while True:
                line = f.readline()
                if not line:
                    break

                temp = line.strip().split(',')
                current_baseId = int(temp[2])

                if current_baseId == last_baseId:
                    end_time = int(temp[4])
                else:
                    break

            out.write('%d,%d,%d,%d,%d\r\n'%(userId, new_cluster_count, last_baseId, start_time, end_time))
